﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.RightsManagement;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.Form;

namespace Halcon二次开发.Tools
{
    /// <summary>
    /// 存储界面数据的工具类
    /// 存储规则：控件的Tag不能为空，尽量与相关的字符绑定，比如：你的TextBox控件是用来存IP的那么Tag就是IP地址
    /// Tag规则：Tag字符串前面必须将$符号
    /// </summary>
    
    internal class StoreFormHelp
    {
        /// <summary>
        /// 存储的方法
        /// </summary>
        public void Store(Form form,string path)
        {
            Dictionary<string, string> valuePairs=new Dictionary<string, string>();
            foreach (Control item in form.Controls)
            {
                //存储父控件的数据
                if (item.Tag!=null)
                {
                    if (item.Tag.ToString().Length > 0)
                    {
                        string s1 = item.Tag.ToString()[0].ToString();
                        if (s1.Equals("$"))
                        {
                            valuePairs.Add(item.Tag.ToString(), item.Text);
                        }
                    }
                }
                StoreNext(item, valuePairs,form.Text);
            }
            if (valuePairs.Keys.Count>0)
            {
                HelpJsons.Write(valuePairs, path);
            }
        }
        //递归存子节点
        public void StoreNext(Control c, Dictionary<string, string> valuePairs, string addres)
        {
            foreach (Control z in c.Controls)
            {
                if (z.Tag != null)
                {
                    if (z.Tag.ToString().Length > 0)
                    {
                        string s1=z.Tag.ToString();
                        string s2 = s1[0].ToString();
                        if (s2.Equals("$"))
                        {
                            valuePairs.Add(z.Tag.ToString(), z.Text);
                        }
                    }
                }
                StoreNext(z, valuePairs, addres);
            }
        }
        /// <summary>
        /// 将数据映射到界面上，上一次存储的界面数据
        /// </summary>
        public void MemoryMap(Form form,string path)
        {
            Dictionary<string, string> dis = HelpJsons.Read(path);
            if (dis != null)
            {
                //判断有没有这个文件Json
                if (File.Exists(path))
                {
                    foreach (Control item in form.Controls)
                    {
                        if (item.Tag != null)
                        {
                            string s1 = item.Tag.ToString()[0].ToString();
                            if (s1.Equals("$"))
                            {
                                foreach (var key in dis)
                                {
                                    if (item.Tag.ToString().Equals(key.Key))
                                    {
                                        item.Text = key.Value;
                                    }
                                }
                            }
                        }
                        MemoryMapNext(item, dis);
                    }
                }
            }
        }
        //递归找字节点
        public void MemoryMapNext(Control c, Dictionary<string, string> valuePairs)
        {
            foreach (Control item in c.Controls)
            {
                if (item.Tag != null)
                {
                    string s1 = item.Tag.ToString()[0].ToString();
                    if (s1.Equals("$"))
                    {
                        foreach (var key in valuePairs)
                        {
                            if (item.Tag.ToString().Equals(key.Key))
                            {
                                item.Text = key.Value;
                            }
                        }
                    }
                }
                MemoryMapNext(item, valuePairs);
            }
        }

    }
}
